<%--
  User: 19079,Date: 2020/12/17,time: 17:59
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <title>杆塔管理</title>
    <base href="<%=request.getContextPath()%>/">
    <link rel="stylesheet" href="webjars/layui/2.5.5/css/layui.css">
</head>
<body>
<%--折叠面板--%>
<div class="layui-collapse" lay-accordion>
    <div class="layui-colla-item">
        <h2 class="layui-colla-title">查询</h2>
        <div class="layui-colla-content layui-show">
            <form class="layui-form"> <!-- 提示：如果你不想用form，你可以换成div等任何一个普通元素 -->
                <div class="layui-form-item layui-input-inline">
                    <label class="layui-form-label">杆塔名称:</label>
                    <div class="layui-input-inline">
                        <input type="text" name="polecode" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-input-inline">
                    <label class="layui-form-label">所属线路：</label>
                    <div class="layui-input-inline">
                        <select name="lineid" lay-filter="aihao">
                            <option value="-1">请选择</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item layui-input-inline">
                    <label class="layui-form-label">是否停用：</label>
                    <div class="layui-input-inline">
                        <select name="isstart" lay-filter="aihao">
                            <option value="-1">请选择</option>
                            <option value="0">停用</option>
                            <option value="1">启用</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item layui-input-inline">
                    <div class="layui-input-inline">
                        <button class="layui-btn"id="ss" type="button" lay-submit lay-filter="formDemo">立即提交</button>
                        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<div class="">
    <table id="towerTB" lay-filter="towerTB"></table>
</div>
<%--修改和删除按钮--%>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="update">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<%--添加和批量删除按钮--%>
<script type="text/html" id="add">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="add" onclick="delet()">批量删除</button>
        <button class="layui-btn layui-btn-sm" lay-event="add" onclick="add()">添加</button>
    </div>
</script>
<%--添加表单弹出层--%>
<div id="addf" style="display: none">
    <form class="layui-form" lay-filter="addf" style="padding: 40px"> <!-- 提示：如果你不想用form，你可以换成div等任何一个普通元素 -->
        <div class="layui-form-item">
            <label class="layui-form-label">用户编码:</label>
            <div class="layui-input-block">
                <input type="text" name="qian"  class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">添加个数:</label>
            <div class="layui-input-block">
                <input type="number" name="num"  class="layui-input">
            </div>
        </div>
    </form>
</div>
<%--修改表单弹出层--%>
<div id="up" style="display: none">
    <form class="layui-form" lay-filter="up" style="padding: 40px"> <!-- 提示：如果你不想用form，你可以换成div等任何一个普通元素 -->
        <div class="layui-form-item layui-input-inline" >
            <label class="layui-form-label">所属线路:</label>
            <input type="hidden" name="id">
            <div class="layui-input-block" style="margin-right: 0px">
                <select name="lineid" lay-filter="aihao">
                    <option value="-1">请选择</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">是否启用:</label>
            <div class="layui-input-block">
                <input type="hidden" name="isstart1">
                <input type="checkbox"  name="isstart"lay-text="ON|OFF"  lay-skin="switch">
            </div>
        </div>
    </form>
</div>
</body>
<script src="webjars/layui/2.5.5/layui.js"></script>
<script>
    layui.use(["jquery"],function () {
        let $=layui.$;
        $(function () {
            $.post("gn/xlgl/getAll",function ({data}) {
                $.each(data,function (index) {
                    $("<option value='"+this.id+"' >" +
                        ""+this.linecode+this.linename+"</option>")
                        .appendTo("[name=lineid]");
                })
            });
        });
        $("#ss").click(function () {
            let lineid=$("[name=lineid]").val();
            let isstart=$("[name=isstart]").val();
            let polecode=$("[name=polecode]").val();
            console.log(polecode);
            layui.table.reload('towerTB', {
                where: { //设定异步数据接口的额外参数，任意设
                    lineid: lineid,
                    isstart:isstart,
                    polecode:polecode
                }
            });
        });
    })
    layui.use(['table','element','jquery'],function () {
        let table=layui.table;
        let $=layui.$;
        table.render({
            elem:"#towerTB",
            id:"towerTB",
            even:true,
            page:true,
            limit:10,
            limits:[10,20,30],
            url:"gn/tggl/getAll",
            toolbar: "#add",
            cols:[[
                {type:"checkbox"},
                {title:"塔杆编号",field:"polecode"},
                {title: "所属线路",templet:function (row) {
                        return row.linename == null ? "暂未分配线路" : row.linename;
                    }},
                {title: "创建时间",field: "creationtime"},
                {title: "创建者",field: "createdby"},
                {title: "最后修改时间",templet:function (row) {
                        return row.lastupdatetime == null ? "暂未修改" : row.lastupdatetime;
                    }},
                {title: "状态",templet:function (row) {
                        return row.isstart == 0 ? "停用" : "启用";
                    }},
                {title: "操作",toolbar: "#barDemo"}
            ]]
        })
        table.on('tool(towerTB)',function ({data,event}){
            switch (event){
                case "update":
                    update(data);
                    break;
                case "del":
                    layui.layer.confirm("确定要删除？",function (index){
                        layui.layer.close(index);
                        //console.log(data.id)
                        del(data.id);
                    });
                    break;
            };
        });
    })
    function add(){
        layui.use(['jquery','layer','form'],function () {
            let layer=layui.layer;
            let $=layui.$;
            $("#addf form")[0].reset();
            layer.open({
                type: 1,
                content:$('#addf'),
                area: '500px',
                btn:['确定',"取消"],
                yes(){
                    let a=layui.form.val('addf');
                    console.log(a);
                    $.post('/gn/tggl/add',{'qian':a.qian,'num':a.num})
                        .done(({msg})=>{
                            layer.close(layer.index);
                            layer.msg(msg);
                            layui.table.reload("towerTB");
                        });
                }
            })
        });
    }
    function del(id){
        layui.use(['layer','jquery','table'],function () {
            let $=layui.$;
            let layer=layui.layer;
            $.post('gn/tggl/del',{"id":id}).done(({msg})=>{
                layer.msg(msg);
                layui.table.reload("towerTB");
            });
        });
    }
    function update(data) {
        layui.use(["layer",'jquery','form'],function () {
            let layer=layui.layer;
            let $=layui.$;
            layui.form.val('up',data);
            layer.open({
                type:1,
                content: $("#up"),
                btn: ["确定","取消"],
                area: "500px",
                yes() {
                    let a=layui.form.val("up");
                    if ($(".layui-form-switch em").text()=="ON"){
                        a.isstart=1;
                    }else {
                        a.isstart=0;
                    }
                    $(".layui-form-switch").removeClass("layui-form-onswitch");
                    layer.close(layer.index);
                    $.post("gn/tggl/update",{'id':a.id,'lineid':a.lineid,
                        "isstart":a.isstart,
                       }).done(({msg})=>{
                        layer.msg(msg);
                        layui.table.reload("towerTB");
                    });
                }
            })
        })
    }
</script>
</html>
